// pages/my/MyConsultation/MyConsultation.js
const app = getApp()
Page({
  data: {
    activeTab: 'completed',  //跳转
    userInfo:'',  // 缓存数据
    appointmentList:[],
    downUrl:'',
    showComment: false,
    showCancelConfirmModal: false,
    commentValue: '',
    ID:''
  },
// 取消预约
showCancelConfirm(e) {
  const id = e.currentTarget.dataset.id;
  this.setData({
    showCancelConfirmModal: true,
    ID:id
  });
},
confirmCancel() {
  // 这里添加实际的取消预约逻辑，如发送请求
  const{ID}=this.data
  wx.request({
    method: 'DELETE',
    url: 'http://localhost:8080/appointment/delete?id='+ID,
    success: (res) => {
      wx.showToast({
        title: '已取消预约',
        icon: 'success',
        success: () => {
          setTimeout(() => {
             this.getData()
          }, 1000);
       }
      });
    },
  })
  this.setData({
    showCancelConfirmModal: false,
    ID: null
  });
},
cancelCancelConfirm() {
  this.setData({
    showCancelConfirmModal: false,
    ID: null
  });
},



  // 评价
  showCommentBox(e) {
    // 获取参数
    const id = e.currentTarget.dataset.id;
    this.setData({
      showComment: true,
      ID:id
    });
  },
  handleCommentInput(e) {
    this.setData({
      commentValue: e.detail.value     
    });
  },
  submitComment() {
    const {commentValue} =this.data
     if (!commentValue) {
      wx.showToast({
          title: '评论内容不能为空',
          icon: 'none'
      });
      return;
  }
    wx.request({
      url: 'http://localhost:8080/appointment/update',
      method: 'PUT',
      data:{
        id:this.data.ID,
        status:3
      },
      success: (res) => {
        wx.showToast({
          title: '评价成功',
          icon: 'success',
          success: () => {
            setTimeout(() => {
               this.getData()
            }, 1000);
         }
        });
      },
    })
    this.setData({
      showComment: false,
      commentValue: ''
    });
  },
  // 已评价按钮
  rated(){
    wx.showToast({
      title: '你已评价',
      icon: 'success',
    });
  },
  cancelComment() {
    this.setData({
      showComment: false,
      commentValue: ''
    });
  },
  switchTab: function (e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
  },
  getData(){
    // 我的咨询表查询
    wx.request({
      url: 'http://localhost:8080/appointment/findByPage',
      method: 'GET',
      data:{
        pageNun:1,
        pageSize:100000,
        userId:this.data.userInfo.userId
      },
      success: (res) => {
        this.setData({
          appointmentList:res.data.data.list
        })
      },
    })
  },
  onLoad() {
    wx.getStorage({
          key: 'userInfo',
          success: (res) => {
              const userInfo = res.data;
              if (!userInfo) {
                  wx.redirectTo({
                      url: '/pages/my/login/login'
                  });
              } else {
                this.setData({
                  userInfo:userInfo,
                  downUrl:app.globalData.downurl
                })
                this.getData()
              }
          },
          fail: (err) => {
              if (err.errMsg === 'getStorage:fail data not found') {
                  // 数据未找到，当作未登录处理，使用 wx.redirectTo 跳转到登录页面
                  console.log("数据未找到");
                  wx.redirectTo({
                      url: '/pages/my/login/login'
                  });
              } else {
                  // 其他错误，给出明确提示
                  wx.showToast({
                      title: '获取本地存储信息出错，请稍后重试',
                      icon: 'none'
                  });
                  console.error('获取本地存储信息失败:', err);
              }
          }
      });
  }
});